package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2022/11/11 15:00
 * @Version 1.0
 */
@Service
public class ArticleDetailService {


    @Autowired
    Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Resource
    private ApArticleMapper articleMapper;


    public void createStaticHtml(String content,Long articleId) throws Exception{

        Template template = configuration.getTemplate("article.ftl");
        Map<String,Object> map = new HashMap<>();
        map.put("content", JSONArray.parseArray(content));
        template.process(map,new FileWriter("D://detail.html"));

        String url = fileStorageService.uploadHtmlFile("", "detail.html", new FileInputStream("D://detail.html"));

        ApArticle article = new ApArticle();
        article.setId(articleId);
        article.setStaticUrl(url);
        articleMapper.updateById(article);
    }
}